package com.beiben.experiment.repair.service.impl;

import java.util.List;

import com.beiben.common.GlobalConstant;
import com.beiben.experiment.equipment.mapper.EquipmentMapper;
import com.beiben.experiment.frock.mapper.FrockMapper;
import com.beiben.experiment.tool.mapper.ToolMapper;
import com.beiben.trialproduce.samplecar.mapper.SampleCarMapper;
import com.ruoyi.common.exception.BaseException;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.beiben.experiment.repair.mapper.RepairMapper;
import com.beiben.experiment.repair.domain.Repair;
import com.beiben.experiment.repair.service.IRepairService;

/**
 * 维修信息Service业务层处理
 * 
 * @author 熊国强
 * @date 2020-11-11
 */
@Service
public class RepairServiceImpl implements IRepairService
{
    @Autowired
    private RepairMapper repairMapper;

    @Autowired
    private EquipmentMapper equipmentMapper;//试验设备\计量设备

    @Autowired
    private FrockMapper frockMapper;//工装

    @Autowired
    private ToolMapper toolMapper;//工具

    @Autowired
    private SampleCarMapper sampleCarMapper;//样车

    /**
     * 查询维修信息
     * 
     * @param repairId 维修信息ID
     * @return 维修信息
     */
    @Override
    public Repair selectRepairById(Long repairId)
    {
        return repairMapper.selectRepairById(repairId);
    }

    /**
     * 查询维修信息列表
     * 
     * @param repair 维修信息
     * @return 维修信息
     */
    @Override
    public List<Repair> selectRepairList(Repair repair)
    {
        switch (repair.getTableName()){
            case "0" ://试验设备
                repair.setTableName(GlobalConstant.EXPERIMENT_EQUIPMENT);
                break;
            case "1" ://工装设备
                repair.setTableName(GlobalConstant.EXPERIMENT_FROCK);
                break; //可选
            case "2" ://工具
                repair.setTableName(GlobalConstant.EXPERIMENT_TOOL);
                break;
            case "3" ://样车
                repair.setTableName(GlobalConstant.TRIAL_PRODUCE_SAMPLE_CAR);
                break;
        }
        repair.setIsFinish("N");
        return repairMapper.selectRepairList(repair);
    }

    /**
     * 新增维修信息
     * 
     * @param repair 维修信息
     * @return 结果
     */
    @Override
    public int insertRepair(Repair repair)
    {
        int result = 0;
        if(StringUtils.isEmpty(repair.getTableName())){
            throw new BaseException("表名不能为空！");
        }
        if(null==repair.getRelIds()){
            throw new BaseException("关联ID不能为空！");
        }else{
            Long[] ids = repair.getRelIds();
            switch (repair.getTableName()){
                case "0" ://试验设备
                    repair.setTableName(GlobalConstant.EXPERIMENT_EQUIPMENT);
                    equipmentMapper.updateEquipmentState(ids,"1");
                    break;
                case "1" ://工装设备
                    repair.setTableName(GlobalConstant.EXPERIMENT_FROCK);
                    frockMapper.updateFrockState(ids,"1");
                    break; //可选
                case "2" ://工具
                    repair.setTableName(GlobalConstant.EXPERIMENT_TOOL);
                    toolMapper.updateToolState(ids,"1");
                    break;
                case "3" : //样车
                    repair.setTableName(GlobalConstant.TRIAL_PRODUCE_SAMPLE_CAR);
                    sampleCarMapper.updateSampleCarState(repair.getRelIds(),repair.getEquipmentState(),repair.getEquipmentUseState());
                    break;
            }
            for(Long id : ids){
                repair.setRelId(id);
                result = repairMapper.insertRepair(repair);
            }
        }
        return result;
    }

    /**
     * 修改维修信息
     * 
     * @param repair 维修信息
     * @return 结果
     */
    @Override
    public int updateRepair(Repair repair)
    {
        return repairMapper.updateRepair(repair);
    }

    /**
     * 批量删除维修信息
     * 
     * @param repairIds 需要删除的维修信息ID
     * @return 结果
     */
    @Override
    public int deleteRepairByIds(Long[] repairIds)
    {
        return repairMapper.deleteRepairByIds(repairIds);
    }

    /**
     * 删除维修信息信息
     * 
     * @param repairId 维修信息ID
     * @return 结果
     */
    @Override
    public int deleteRepairById(Long repairId)
    {
        return repairMapper.deleteRepairById(repairId);
    }

    /**
     * 修改维修状态信息
     *
     * @param repair 维修信息
     * @return 结果
     */
    @Override
    public int updateRepairIsFinish(Repair repair) {
        return repairMapper.updateRepairIsFinish(repair);
    }
}
